package com.yswy.common.utils;

import java.security.SecureRandom;
import java.util.Random;
import java.util.UUID;

public class RandomUtil {

	private final static char[] numbers = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
	private final static char[] numbersZero = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
	private final static char[] numbers16 = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	private final static String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

	private static String[] chars = new String[] {
		"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
		"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
		"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C",
		"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
		"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

	private static long[] swctb = {
		18, 141, 181, 218, 203, 187, 212, 40, 126, 46, 22, 158, 9, 26, 65, 255, 190, 241,
		242, 156, 66, 37, 68, 106, 143, 146, 167, 159, 48, 61, 25, 60, 192, 32, 127, 101,
		105, 231, 29, 82, 148, 182, 204, 59, 134, 54, 169, 125, 110, 47, 163, 36, 248, 221,
		89, 117, 188, 131, 237, 116, 7, 63, 58, 247, 185, 174, 14, 96, 98, 88, 161, 6, 107,
		90, 130, 118, 34, 177, 51, 164, 245, 53, 73, 112, 166, 224, 43, 210, 8, 186, 12,
		183, 173, 39, 71, 10, 140, 227, 17, 132, 78, 234, 149, 20, 93, 228, 196, 243, 84,
		205, 137, 81, 252, 175, 104, 144, 87, 50, 150, 70, 103, 23, 157, 109, 230, 219, 209,
		155, 80, 184, 28, 172, 44, 191, 31, 121, 99, 86, 160, 222, 240, 216, 151, 246, 254,
		38, 147, 62, 197, 91, 100, 207, 238, 3, 95, 4, 102, 226, 113, 202, 35, 253, 76, 215,
		168, 79, 119, 45, 19, 24, 1, 235, 142, 162, 16, 233, 176, 223, 171, 55, 122, 52, 0,
		193, 33, 217, 135, 49, 13, 94, 170, 72, 225, 27, 213, 199, 111, 239, 244, 153, 67,
		208, 21, 30, 139, 92, 198, 201, 77, 232, 2, 220, 249, 180, 57, 133, 229, 115, 138,
		194, 85, 211, 97, 152, 178, 56, 251, 83, 69, 206, 64, 189, 15, 108, 179, 250, 74,
		136, 42, 11, 41, 195, 114, 214, 5, 154, 129, 120, 236, 75, 200, 128, 123, 145, 165, 124
	};

	private static Random r = new Random();
	private static SecureRandom sr = new SecureRandom();

	/**
	 * 生成短8位UUID,通过随机生成32位UUID，由于UUID都为十六进制，所以将UUID分成8组，每4个为一组，然后通过模62操作，结果作为索引取出字符
	 * @return
	 */
	public static String genShortUuid() {
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		return shortBuffer.toString().toUpperCase();
	}
	
	  /**
		 * 生成数字字母组合字符串
		 * @param length
		 * @return
		 */
		public static String getExchangeCode(int length) {
			String combination = "";
			String charOrNum = "char";
			Random random = new Random();
			for (int i = 0; i < length; i++) {
				if (i == 0) {
					charOrNum = "char";
				}else if (i == length-1) {
					charOrNum = "num";
				}else {
					charOrNum = random.nextInt(2)%2==0?"char":"num";
				}
				// 获得随机的数字
				int y = 0;
				if (charOrNum.equals("num")) {
					y = random.nextInt(10);
					if (y==0) { y=3; }  // 0和O不易区分
					if (y==1) { y=9; }  // 1和I不易区分
					combination += String.valueOf(y);
				}else {// 获得随机的 大写字母
					y = 65+random.nextInt(26);
					if (y==79) { y=75; }    // 0和O不易区分
					combination += (char) (y);
				}
			}
			return combination;
		}

	/**
	 * 随机生成32位UUID
	 * @return
	 */
	public static String genUuid() {
		return UUID.randomUUID().toString().replace("-", "").toUpperCase();
	}

	public static int genRadom(int min, int max) {
		Random r = new Random();
		int tmp = Math.abs(r.nextInt());
		return tmp % (max - min + 1) + min;
	}

	public static String getVerifyCode() {
		StringBuilder sb = new StringBuilder(4);
		for (int i = 0; i < 4; i++) {
			sb.append(numbers[r.nextInt(9)]);
		}
		return sb.toString();
	}

	public static String getTempPassword() {
		StringBuilder sb = new StringBuilder(6);
		for (int i = 0; i < 6; i++) {
			sb.append(numbers[r.nextInt(9)]);
		}
		return sb.toString();
	}

	public static String getRandomKey() {
		StringBuilder sb = new StringBuilder(32);
		for (int i = 0; i < 32; i++) {
			sb.append(numbers16[sr.nextInt(numbers16.length)]);
		}
		return sb.toString();
	}

	public static String getRandomInt() {
		StringBuilder sb = new StringBuilder(8);
		for (int i = 0; i < 8; i++) {
			sb.append(numbers16[sr.nextInt(numbers16.length)]);
		}
		return sb.toString();
	}

	public static String getRandomKey16() {
		StringBuilder sb = new StringBuilder(16);
		for (int i = 0; i < 16; i++) {
			sb.append(numbers16[sr.nextInt(numbers16.length)]);
		}
		return sb.toString();
	}

	public static String getPassCode() {
		StringBuilder sb = new StringBuilder(8);
		for (int i = 0; i < 8; i++) {
			sb.append(numbersZero[sr.nextInt(numbersZero.length)]);
		}
		return sb.toString();
	}

	public static String getRandomNumber(int len) {
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			sb.append(numbersZero[sr.nextInt(numbersZero.length)]);
		}
		return sb.toString();
	}

	public static String getMessengerCode() {
		StringBuilder sb = new StringBuilder();
		int length = base.length();
		for (int i = 0; i < 8; i++) {
			// 得到随机产生的验证码
			int start = r.nextInt(length);
			String strRand = base.substring(start, start + 1);
			sb.append(strRand);
		}
		return sb.toString();
	}

	public static String getRandomNumber(long id, int size) {
		long srci = (id & 0xFFFFFF00) + swctb[(int) ((id) & 0xff)];
		String src = String.valueOf(srci);
		String l = String.valueOf(src.length());
		String ret = "";
		Random r = new Random();
		for (int i = 0; i < size - (src.length() + l.length()); i++) {
			ret += r.nextInt(9);
			if (i == 2) {
				ret += l;
			}
		}
		ret += src;
		int len = ret.length();
		if (len > size) {
			ret = ret.substring(len - size, len);
		} else {
			ret = "0000000000".substring(0, size - len) + ret;
		}
		return ret;
	}


	/**
	 * 生成数字字母组合字符串
	 * @param length
	 * @return
	 */
	public static String getCombination(int length) {
		String combination = "";
		String charOrNum = "char";
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			if (i == 0) {
				charOrNum = "char";
			}else if (i == length-1) {
				charOrNum = "num";
			}else {
				charOrNum = random.nextInt(2)%2==0?"char":"num";
			}
			// 获得随机的数字
			int y = 0;
			if (charOrNum.equals("num")) {
				y = random.nextInt(10);
				if (y==0) { y=3; }  // 0和O不易区分
				if (y==1) { y=9; }  // 1和I不易区分
				combination += String.valueOf(y);
			}else {// 获得随机的 大写字母
				y = 65+random.nextInt(26);
				if (y==79) { y=75; }    // 0和O不易区分
				combination += (char) (y);
			}
		}
		return combination;
	}
}